package message;

import entity.ChessUser;
import model.RoomInfo;
import socket.ChessServer;

/**
 * 点击房间左右进入报文.
 *
 * @author 张紘溢
 */
public class ClickRoomMessage extends BaseMessage {

    private int roomId;
    private ChessUser chessUser;
    private boolean isLeft;

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    public ChessUser getUser() {
        return chessUser;
    }

    public void setUser(ChessUser chessUser) {
        this.chessUser = ChessServer.getInstance().getChessUserService().findUser(chessUser.getName());
    }

    public boolean isLeft() {
        return isLeft;
    }

    public void setLeft(boolean left) {
        this.isLeft = left;
    }

    public ClickRoomMessage(int roomId, ChessUser chessUser, boolean isLeft) {
        super();
        this.roomId = roomId;
        this.chessUser = ChessServer.getInstance().getChessUserService().findUser(chessUser.getName());
        this.isLeft = isLeft;
    }

    @Override
    public void sendToServer() {
        RoomInfo roomInfo = ChessServer.getInstance().getRooms().get(roomId);
        System.out.println("进入房间前======" + roomInfo);
        if (roomInfo.getStatus() == RoomInfo.PLAYING) {//如果房间有两人,什么都不做
            return;
        }
        if (roomInfo.getStatus() == RoomInfo.NONE) {//如果房间为空
            roomInfo.setStatus(roomInfo.WAIT);
            if (isLeft) {
                roomInfo.setLeftPlayer(chessUser);
            } else {
                roomInfo.setRightPlayer(chessUser);
            }
            //更新玩家自己的界面,进入房间
            ServerEnterRoomMessage serverEnterRoomMessage = new ServerEnterRoomMessage(roomId, isLeft, chessUser);
            ChessServer.getInstance().sendMsgToClient(serverEnterRoomMessage, this.sender.getClient());
            //更新房间列表
            ServerRoomListMessage serverRoomListMessage = new ServerRoomListMessage(ChessServer.getInstance().getRooms());
            ChessServer.getInstance().sendMsgToAll(serverRoomListMessage);
            //更新玩家进入房间的界面信息
            ServerRoomPlayerMessage serverRoomPlayerMessage = new ServerRoomPlayerMessage(roomInfo);
            ChessServer.getInstance().sendMsgToAll(serverRoomPlayerMessage);
        }
        if (roomInfo.getStatus() == RoomInfo.WAIT) {//如果房间有一人
            if (isLeft) {
                if (roomInfo.getLeftPlayer() != null) {
                    return;
                }
                roomInfo.setStatus(RoomInfo.PLAYING);
                roomInfo.setLeftPlayer(chessUser);
                roomInfo.setRightPlayer(ChessServer.getInstance().getChessUserService().findUser(roomInfo.getRightPlayer().getName()));
                ServerEnterRoomMessage serverEnterRoomMessage = new ServerEnterRoomMessage(roomId, isLeft, chessUser);
                ChessServer.getInstance().sendMsgToClient(serverEnterRoomMessage, this.sender.getClient());
                ServerRoomListMessage serverRoomListMessage = new ServerRoomListMessage(ChessServer.getInstance().getRooms());
                ChessServer.getInstance().sendMsgToAll(serverRoomListMessage);
                ServerRoomPlayerMessage serverRoomPlayerMessage = new ServerRoomPlayerMessage(roomInfo);
                ChessServer.getInstance().sendMsgToAll(serverRoomPlayerMessage);
            } else {
                if (roomInfo.getRightPlayer() != null) {
                    return;
                }
                roomInfo.setStatus(RoomInfo.PLAYING);
                roomInfo.setRightPlayer(chessUser);
                roomInfo.setLeftPlayer(ChessServer.getInstance().getChessUserService().findUser(roomInfo.getLeftPlayer().getName()));
                ServerEnterRoomMessage serverEnterRoomMessage = new ServerEnterRoomMessage(roomId, isLeft, chessUser);
                ChessServer.getInstance().sendMsgToClient(serverEnterRoomMessage, this.sender.getClient());
                ServerRoomListMessage serverRoomListMessage = new ServerRoomListMessage(ChessServer.getInstance().getRooms());
                ChessServer.getInstance().sendMsgToAll(serverRoomListMessage);
                ServerRoomPlayerMessage serverRoomPlayerMessage = new ServerRoomPlayerMessage(roomInfo);
                ChessServer.getInstance().sendMsgToAll(serverRoomPlayerMessage);
            }
        }
    }

    @Override
    public void selfExecute() {

    }
}
